package ai.tuobot.sdk.util

import android.util.Log
import org.json.JSONObject
import java.util.Locale

object WordsMatch {
    private val TAG = "WordsMatch"
    fun matching(word:String, recMapRes:HashMap<String, ArrayList<JSONObject>>): Boolean{
        if (word.isEmpty()) return false
        Log.d(TAG, "matching word: $word")
        val toCompareWord = word.lowercase(Locale.getDefault())
        val onFinalResultList = recMapRes["onFinalResult"]
        if (onFinalResultList != null) {
            for (i in onFinalResultList.size - 1 downTo 0) {
                val onFinalResult = onFinalResultList[i]
                if (onFinalResult.has("text") && onFinalResult.getString("text").contains(toCompareWord)){
                    return true
                }
            }
        }
        val onPartialResultList = recMapRes["onPartialResult"]
        if (onPartialResultList != null) {
            for (i in onPartialResultList.size - 1 downTo 0) {
                val onPartialResult = onPartialResultList[i]
                if (onPartialResult.has("partial") && onPartialResult.getString("partial").contains(toCompareWord)){
                    return true
                }
            }
        }
        return false
    }
}